import pymongo as pg
import tushare as ts
import datetime
import gzip
import time
import math
import traceback
import os
import pandas as pd
import numpy as np
from operator import itemgetter
import operator
import urllib.request
import json

# 链接数据库集合
def conDb(dbName, collectionName):
	client = pg.MongoClient('localhost',27017, connect=False)
	db = client.get_database(dbName)
	collection = db.get_collection(collectionName)
	return collection

# 将当天所有股票行情入库,增量的数据,每天跑一次
def saveTodayAllStock():
	collection = conDb('Today_All_Stock','today_all_stocks')
	codeAndStock = conDb('Code_Name_Stock', 'code_name_stocks')
	collectionHistoryStock = conDb('Hist_Stock', 'get_histIncrease_stocks')
	todayDate = datetime.datetime.today().date()
	todayHistoryInfo = collectionHistoryStock.find({'date': str(todayDate)})
	if todayHistoryInfo.count() != 0:
		collectionHistoryStock.remove({'date': str(todayDate)})
	collection.drop()
	codeAndStock.drop()
	list = np.array(ts.get_today_all()).tolist()
	list.sort(key = operator.itemgetter(2), reverse = True)
	for item in list:
		collection.insert({'date':str(datetime.datetime.today().date()),'code':item[0],'name': item[1],'changepercent': item[2],'trade': item[3],'open': item[4],\
						   'high': item[5],'low': item[6],'settlement': item[7],'volume': item[8],'turnoverratio': item[9],\
						   'amount': item[10],'per': item[11],'pb': item[12],'mktcap': item[13],'nmc': item[14]})
	# 存入日积累的历史数据
	for item in list:
		collectionHistoryStock.insert({'date':str(datetime.datetime.today().date()),'code':item[0],'name': item[1],'changepercent': item[2],'trade': item[3],'open': item[4],\
						   'high': item[5],'low': item[6],'settlement': item[7],'volume': item[8],'turnoverratio': item[9],\
						   'amount': item[10],'per': item[11],'pb': item[12],'mktcap': item[13],'nmc': item[14]})
	# 存入股票代码和名称
	for item in list:
		codeAndStock.insert({'date':str(datetime.datetime.today().date()),'code':item[0],'name': item[1]})
# 查询所有的股票代码及名称
def queryAllStockCodeAndName():
	allStockList = []
	collection = conDb('Code_Name_Stock','code_name_stocks')
	allStock = collection.find({},{'_id':0,'date':1,'code':1,'name':1})
	for item in allStock:
		if item not in allStockList:
			allStockList.append(item)
	return allStockList

# 入库3年之内的历史数据
def saveHistScodes():
	begin = datetime.date(2008,1,10)
	end = datetime.date(2019,5,10)
	mis = datetime.timedelta(days=1)
	todayDate = datetime.datetime.today().date()
	collection = conDb('Hist_Stock','get_hist_stocks')
	todayInfo = collection.find({'date': str(todayDate)})
	if todayInfo.count() != 0:
		collection.remove({'date':str(todayDate)})
	for item in queryAllStockCodeAndName():
		code = item.get('code')
		histMetadata = ts.get_hist_data(code, start=str(begin), end=str(end))
		list = np.array(histMetadata).tolist()
		try:
			turnover = getTurnoverByTodayStock(code)
			if(turnover == None):
				turnover = 0
		except BaseException:
			turnover = 0
		try:
			capital_list = get_capital_flow(code)
			if(len(capital_list) <= 0):
				capital_list = [0 for i in range(6)]
		except BaseException:
			capital_list = [0 for i in range(6)]
		try:
			dateList = (histMetadata).index.values.tolist()
			print(dateList)
		except BaseException:
			continue
		if(len(list) > 0):
			for date,i in zip(dateList,list):
				collection.insert({'date':str(date),'code':code,'open':i[0],'high':i[1],'close':i[2],'low':i[3],'volume':i[4],
								   'price_change':i[5],'p_change':i[6],'ma5':i[7],'ma10':i[8],'ma20':i[9],'v_ma5':i[10],
								   'v_ma10':i[11],'v_ma20':i[12],'turnover':turnover,'main_in':capital_list[0],'main_out':capital_list[1],
								   'main_net_in':capital_list[2],'private_in':capital_list[3],'private_out':capital_list[4],'private_net_in':capital_list[5]})

# 从历史数据表中查询某一只股票的历史数据
def queryOneStockHistData(stockCode,date):
	oneStockHistData = []
	collection = conDb('Hist_Stock','get_hist_stocks')
	oneStockHistData.append([item for item in collection.find({'code':stockCode,'date':date}) if item is not None][0])
	return oneStockHistData
# 从数据行情历史积累表中查询某只股票的历史数据
def queryOneStockToHistData(stockCode,date):
	oneStockToHistData = []
	collection = conDb('Today_All_Stock','today_all_stocks')
	oneStockToHistData.append([item for item in collection.find({'code':stockCode,'date':date}) if item is not None][0])
	return oneStockToHistData
#查询历史股票中查询某一只股票的所有历史数据
def queryOneStockHistAllDateData(stockCode):
	collection = conDb('Hist_Stock', 'get_hist_stocks')
	oneStockHistAllDateData = [item for item in collection.find({'code':stockCode}).sort('date',1).limit(10)]
	return oneStockHistAllDateData

#通过日积累股票中查询某一只股票的所有历史数据
def queryOneStockHistAllDateIncreaseData(stockCode):
	collection = conDb('Hist_Stock', 'get_histIncrease_stocks')
	oneStockHistAllDateIncreaseData = [item for item in collection.find({'code':stockCode}).sort('date',1).limit(10)]
	return oneStockHistAllDateIncreaseData

# 通过url获取当天的换手率
def getTurnoverByUrl(code):
	html = urllib.request.urlopen('http://qt.gtimg.cn/q=sh'+code)
	result = html.read()
	resultTow = str(result)
	stringList = resultTow.split("~")
	turnover = float(stringList[38])
	return turnover

# 根据当天实时股票数据获取换手率
def getTurnoverByTodayStock(stockCode):
	collection = conDb("Today_All_Stock","today_all_stocks")
	oneStockTurnover = [item for item in collection.find({'code':stockCode})]
	turnover = oneStockTurnover[0].get("turnoverratio")
	return turnover

#通过url获取当天主力流入流出量
def get_capital_flow(code):
	capital_list = []
	html = urllib.request.urlopen('http://qt.gtimg.cn/q=ff_sh' + code)
	result = html.read()
	resultTow = str(result)
	stringList = resultTow.split("~")
	main_in = float(stringList[1])
	main_out = float(stringList[2])
	main_net_in = float(stringList[3])
	private_in = float(stringList[5])
	private_out = float(stringList[6])
	private_net_in = float(stringList[7])
	capital_list.append(main_in)
	capital_list.append(main_out)
	capital_list.append(main_net_in)
	capital_list.append(private_in)
	capital_list.append(private_out)
	capital_list.append(private_net_in)
	return capital_list

